using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace PropertyBagFeatureSample.ApplicationPages
{
    public class PropertyBagEditor : WebAdminPageBase
    {
        public PropertyBagEditor()
        {
        }

        protected Button buttonOK;
        
        protected Button buttonUpdate;
        protected Button buttonDelete;

        protected GridView gridView1;

        protected override bool RequireSiteAdministrator { get { return true; } }


        public static System.Data.DataSet GetProperties()
        {
            try
            {
                DataSet dataSet = new DataSet();
                DataTable dataTable = new DataTable("PropertyBag");
                dataSet.Tables.Add(dataTable);

                DataColumn dc = new DataColumn("Property", System.Type.GetType("System.String"));
                dataSet.Tables["PropertyBag"].Columns.Add(dc);

                dc = new DataColumn("Value", System.Type.GetType("System.String"));
                dataSet.Tables["PropertyBag"].Columns.Add(dc);



                SPWeb web = SPContext.Current.Web;
                foreach (string key in web.Properties.Keys)
                {
                    DataRow row = dataTable.NewRow();
                    row["Property"] = key;
                    row["Value"] = web.Properties[key];
                    dataTable.Rows.Add(row);
                }
                return dataSet;
            }
            catch
            { }
            return null;
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {
                gridView1.DataSource = GetProperties();
                gridView1.DataBind();

            }
        }

        protected void HandleOKButtonClick(object sender, EventArgs e)
        {
            // Redirect to the site's settings page.
            SPUtility.Redirect("UpdateProperty.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }


        protected void HandleUpdateButtonClick(object sender, EventArgs e)
        {
            // Redirect to the site's settings page.
            SPUtility.Redirect("UpdateProperty.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }

        protected void HandleDeleteButtonClick(object sender, EventArgs e)
        {
            // Redirect to the site's settings page.
            SPUtility.Redirect("DeleteProperty.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }



    }

       public class UpdateProperty : WebAdminPageBase
    {
        public UpdateProperty()
        {
        }

        protected Button buttonOK;
        protected TextBox propertyNameTextBox;
        protected TextBox propertyValueTextBox;

        protected override bool RequireSiteAdministrator { get { return true; } }


        public static void UpdateToPropertyBag(String propertyName,string propertyValue)
        {
            SPWeb web = SPContext.Current.Web;
            if (web != null)
            {
                if (web.Properties.ContainsKey(propertyName))
                {
                    web.Properties[propertyName] = propertyValue;
                }
                else
                {
                    web.Properties.Add(propertyName, propertyValue);
                    
                }
                web.Properties.Update();
            }
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {
      
            }
        }

        protected void HandleOKButtonClick(object sender, EventArgs e)
        {

            UpdateToPropertyBag(propertyNameTextBox.Text, propertyValueTextBox.Text);
            // Redirect to the site's settings page.
            SPUtility.Redirect("PropertyBag.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }

        protected void HandleCancelButtonClick(object sender, EventArgs e)
        {
            // Redirect to the site's settings page.
            SPUtility.Redirect("PropertyBag.aspx", SPRedirectFlags.UseSource | SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }



    }
}
